﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  [DebuggerStepThrough]
  internal abstract class ContextualParentDataHelper<TParentElement, TChildElement, TGrandChildElement> : IContextualDataHelper<TChildElement, TParentElement>
  {
    #region Constructor

    protected ContextualParentDataHelper( IContextualDataHelper<TGrandChildElement, TChildElement> childDataHelper )
    {
      if( childDataHelper == null )
        throw new ArgumentNullException( "childDataHelper" );

      m_childDataHelper = childDataHelper;
    }

    #endregion

    #region ChildDataHelper Propected Property

    protected IContextualDataHelper<TGrandChildElement, TChildElement> ChildDataHelper
    {
      get
      {
        return m_childDataHelper;
      }
    }

    private readonly IContextualDataHelper<TGrandChildElement, TChildElement> m_childDataHelper;

    #endregion

    #region IContextualItemHelper<TParentElement> Members

    public virtual object GetMemberValue(
      DataSourceContext context,
      string propertyPath,
      TParentElement item )
    {
      TChildElement childItem = this.ConvertBack( context, item );

      return m_childDataHelper.GetMemberValue( context, propertyPath, childItem );
    }

    public virtual bool TryGetMemberValue(
      DataSourceContext context,
      string propertyPath,
      TParentElement item,
      out object value )
    {
      TChildElement childItem = this.ConvertBack( context, item );

      return m_childDataHelper.TryGetMemberValue( context, propertyPath, childItem, out value );
    }

    #endregion

    #region IContextualConvertHelper<TChildElement, TParentElement> Members

    public abstract TParentElement Convert( DataSourceContext context, TChildElement value );
    public abstract TChildElement ConvertBack( DataSourceContext context, TParentElement value );

    public virtual TParentElement DeepConvert( DataSourceContext context, object value )
    {
      TChildElement childItem = m_childDataHelper.DeepConvert( context, value );

      return this.Convert( context, childItem );
    }

    public virtual object DeepConvertBack( DataSourceContext context, TParentElement value )
    {
      TChildElement childItem = this.ConvertBack( context, value );

      return m_childDataHelper.DeepConvertBack( context, childItem );
    }

    #endregion

    #region IContextualCompareHelper<TParentElement> Members

    public virtual int Compare( DataSourceContext context, TParentElement firstItem, TParentElement secondItem )
    {
      TChildElement firstChildItem = this.ConvertBack( context, firstItem );
      TChildElement secondChildItem = this.ConvertBack( context, secondItem );

      return m_childDataHelper.Compare( context, firstChildItem, secondChildItem );
    }

    public virtual int Compare( DataSourceContext context, TParentElement item, SourcePosition position )
    {
      if( position.IsBeforeFirst )
        return 1;

      if( position.IsAfterLast )
        return -1;

      ItemPosition itemPosition = position as ItemPosition;
      if( itemPosition != null )
        return this.Compare( context, item, ( TParentElement )itemPosition.Item );

      TChildElement childItem = this.ConvertBack( context, item );

      return m_childDataHelper.Compare( context, childItem, position );
    }

    public int Compare( DataSourceContext context, SourcePosition position, TParentElement item )
    {
      return -this.Compare( context, item, position );
    }

    public int Compare( DataSourceContext context, SourcePosition firstPosition, SourcePosition secondPosition )
    {
      if( object.ReferenceEquals( firstPosition, secondPosition ) )
        return 0;

      if( firstPosition.IsBeforeFirst )
        return -1;

      if( firstPosition.IsAfterLast )
        return 1;

      ItemPosition firstItemPosition = firstPosition as ItemPosition;
      if( firstItemPosition != null )
        return this.Compare( context, ( TParentElement )firstItemPosition.Item, secondPosition );

      ItemPosition secondItemPosition = secondPosition as ItemPosition;
      if( secondItemPosition != null )
        return this.Compare( context, firstPosition, ( TParentElement )secondItemPosition.Item );

      return m_childDataHelper.Compare( context, firstPosition, secondPosition );
    }

    public int Compare( DataSourceContext context, string propertyPath, object firstValue, object secondValue )
    {
      return m_childDataHelper.Compare( context, propertyPath, firstValue, secondValue );
    }

    #endregion

    #region IContextualEditHelper<TParentElement> Members

    public virtual void Invalidate( DataSourceContext context, TParentElement item )
    {
      TChildElement childItem = this.ConvertBack( context, item );

      m_childDataHelper.Invalidate( context, childItem );
    }

    #endregion
  }
}
